// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Sliding Floor Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 76 : TYPE_SLIDINGFLOOR

// Function declarations
reserve function SlidingFloor_DebugDraw
reserve function SlidingFloor_DebugSpawn

// Static Values

// Tables

function SlidingFloor_DebugDraw
	DrawSprite(0)
end function


function SlidingFloor_DebugSpawn
	CreateTempObject(TypeName[Sliding Floor], 0, object.xpos, object.ypos)
end function


event ObjectMain
	switch object.state
	case 0
		if object[+1].value0 == 1
			object.state++
			object.priority = PRIORITY_ACTIVE
			object.value0 = 256
			object[309].state = 3
			object[314].state = 3		
		end if
	case 2
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_BOX, object.entityPos, -128, -64, 128, 64, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			BoxCollisionTest(C_TOUCH, object.entityPos, -128, -64, 128, 64, currentPlayer, 2, 2, 2, 2)
			if checkResult == 1
				if object[currentPlayer].ypos > object.ypos
					object[currentPlayer].ixpos = object[currentPlayer].collisionRight
					FlipSign(object[currentPlayer].xpos)
					object[currentPlayer].xpos -= 0x800000
					object[currentPlayer].xpos += object.xpos
				else
					object[currentPlayer].iypos = object[currentPlayer].collisionBottom
					FlipSign(object[currentPlayer].ypos)
					object[currentPlayer].ypos -= 0x400000
					object[currentPlayer].ypos += object.ypos
				end if
			end if
			BoxCollisionTest(C_TOUCH, object.entityPos, -130, -63, 130, 63, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				if object[currentPlayer].xpos < object.xpos
					if object[currentPlayer].right == 1
						object[currentPlayer].pushing = 2
						object[currentPlayer].ixpos = object[currentPlayer].collisionRight
						FlipSign(object[currentPlayer].xpos)
						object[currentPlayer].xpos -= 0x800000
						object[currentPlayer].xpos += object.xpos
						if object[currentPlayer].ypos == 0x16D0000
							object[currentPlayer].ypos -= 0x10000
						end if
						if object[currentPlayer].ypos == 0x1710000
							object[currentPlayer].ypos -= 0x10000
						end if
					end if
				end if
			end if
		next
		break
	case 1
		temp0 = object.xpos
		temp0 &= -0x10000
		temp1 = object.ypos
		temp1 &= -0x10000
		object.xpos -= 0x10000
		object.ypos += 0x8000
		object.value0--
		if object.value0 < 0
			object.priority = PRIORITY_ACTIVE_BOUNDS
			object.state++
		end if
		object.value1 = object.xpos
		object.value1 &= -0x10000
		object.value1 -= temp0
		object.value2 = object.ypos
		object.value2 &= -0x10000
		object.value2 -= temp1
		temp2 = object.xpos
		object.xpos = temp0
		temp3 = object.ypos
		object.ypos = temp1
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_BOX, object.entityPos, -128, -64, 128, 64, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				object[currentPlayer].xpos += object.value1
				object[currentPlayer].ypos += object.value2
			end if
			BoxCollisionTest(C_TOUCH, object.entityPos, -128, -64, 128, 64, currentPlayer, 2, 2, 2, 2)
			if checkResult == 1
				if object[currentPlayer].ypos > object.ypos
					object[currentPlayer].ixpos = object[currentPlayer].collisionRight
					FlipSign(object[currentPlayer].xpos)
					object[currentPlayer].xpos -= 0x800000
					object[currentPlayer].xpos += object.xpos
				else
					object[currentPlayer].iypos = object[currentPlayer].collisionBottom
					FlipSign(object[currentPlayer].ypos)
					object[currentPlayer].ypos -= 0x400000
					object[currentPlayer].ypos += object.ypos
				end if
			end if
			BoxCollisionTest(C_TOUCH, object.entityPos, -130, -63, 130, 63, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				if object[currentPlayer].xpos < object.xpos
					if object[currentPlayer].right == 1
						object[currentPlayer].pushing = 2
						object[currentPlayer].ixpos = object[currentPlayer].collisionRight
						FlipSign(object[currentPlayer].xpos)
						object[currentPlayer].xpos -= 0x800000
						object[currentPlayer].xpos += object.xpos
					end if
				end if
			end if
		next
		object.xpos = temp2
		object.ypos = temp3
		break
	end switch
end event


event ObjectDraw
	DrawSprite(0)
end event


event ObjectStartup
	LoadSpriteSheet("LZ/Objects2.gif")
	SpriteFrame(-128, -64, 256, 125, 0, 0)
	SetTableValue(TypeName[Sliding Floor], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(SlidingFloor_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(SlidingFloor_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
